package org.sshbot;

import de.mud.terminal.VT320;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import static org.sshbot.util.LogHelper.DEBUG;

public class GestureListener implements GestureDetector.OnGestureListener {
	
	 private static final int SWIPE_MIN_DISTANCE = 120;
	 private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	 private float totalY = 0;


	 TerminalView view;
	 public GestureListener(TerminalView view) {
         this.view = view;
     }

	 
	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	 @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

       final float distx = e2.getRawX() - e1.getRawX();
       final float disty = e2.getRawY() - e1.getRawY();
       final int goalwidth = view.charWidth / 2;

       // need to slide across half of display to trigger console change
       // make sure user kept a steady hand horizontally
       if (Math.abs(disty) < (view.charHeight / 4)) {
         if (distx > goalwidth) {
           //shiftCurrentTerminal(SHIFT_RIGHT);
            
           return true;
         }

         if (distx < -goalwidth) {
           //shiftCurrentTerminal(SHIFT_LEFT);
             
           return true;
         }

       }

       return false;
     }


	@Override
	public void onLongPress(MotionEvent arg0) {
		DEBUG("Long press!");
	}

	@Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

      
      if (e1 == null || e2 == null) {
        return false;
      }

      // if releasing then reset total scroll
      if (e2.getAction() == MotionEvent.ACTION_UP) {
        totalY = 0;
      }

      // activate consider if within x tolerance
      if (Math.abs(e1.getX() - e2.getX()) < ViewConfiguration.getTouchSlop() * 4) {

    	  if(view == null) return false;
        // estimate how many rows we have scrolled through
        // accumulate distance that doesn't trigger immediate scroll
        totalY += distanceY;
        final int moved = (int) (totalY / view.charHeight);

        // consume as scrollback only if towards right half of screen
        if (e2.getX() > view.charHeight / 2) {
          if (moved != 0) {
	            int base = view.buffer.getWindowBase();
	            view.buffer.setWindowBase(base + moved);
	            totalY = 0;
	            return true;
          }
        } else {
          // otherwise consume as pgup/pgdown for every 5 lines
          if (moved > 5) {
        	  	((VT320)view.buffer).keyPressed(VT320.KEY_PAGE_DOWN, ' ', 0);
	            totalY = 0;
	            return true;
          } else if (moved < -5) {
	        	((VT320)view.buffer).keyPressed(VT320.KEY_PAGE_UP, ' ', 0);
	            totalY = 0;
	            return true;
          }

        }

      }

      return false;
    }


	@Override
	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
}